package com.masterclient.app.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.eoner.baselib.utils.sp.SPUtils;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.eoner.common.config.Config;
import com.masterclient.app.R;
import com.masterclient.app.utils.CommonUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @auther hjian
 * @date 2019/9/8.
 */
public class ImageDialog extends Dialog {


    @BindView(R.id.dialog_vip_img)
    ImageView dialogVipImg;
    @BindView(R.id.dialog_vip_close)
    ImageView dialogVipClose;

    private String imgUrl, target, targetId, title, source;
    private Context context;

    private OnChooseListerner onChooseListerner;


    public interface OnChooseListerner {
        public void cancel();

        public void confirm();
    }

    public ImageDialog(String source, @NonNull Context context, String imgUrl, String target, String targetId, String title) {
        super(context, R.style.MyDialog);
        this.imgUrl = imgUrl;
        this.target = target;
        this.targetId = targetId;
        this.context = context;
        this.title = title;
        this.source = source;
    }

    public ImageDialog(String source, @NonNull Context context, String imgUrl, String target,
                       String targetId, String title, OnChooseListerner onChooseListerner) {
        this(source, context, imgUrl, target, targetId, title);
        this.onChooseListerner = onChooseListerner;
    }

    public void setImageResource(@DrawableRes int imgRes) {
        Glide.with(context).load(imgRes).apply(new RequestOptions().error(R.color.transparent).placeholder(R.color.transparent))
                .into(dialogVipImg);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_vip);
        ButterKnife.bind(this);

        ViewTransformUtil.glideImageView(context, imgUrl, dialogVipImg, R.color.transparent);
        dialogVipClose.setVisibility(View.VISIBLE);
        dialogVipImg.setVisibility(View.VISIBLE);

    }

    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        WindowManager.LayoutParams windowparams = window.getAttributes();
        windowparams.width = ScreenUtils.getScreenWidth();
        window.setGravity(Gravity.CENTER);
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(windowparams);
    }

    @OnClick({R.id.dialog_vip_img, R.id.dialog_vip_close})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.dialog_vip_img:
                if ((!"live_list".equals(target) && !"live_room".equals(target)) ||
                        !TextUtils.isEmpty(SPUtils.getInstance().getString(Config.CUSTOMERID))) {
                    dismiss();
                }

                if (onChooseListerner != null) {
                    onChooseListerner.confirm();
                }
                CommonUtil.goAnyWhere(context, target, targetId, "", title, "");
                break;
            case R.id.dialog_vip_close:
                dismiss();
                if (onChooseListerner != null) {
                    onChooseListerner.cancel();
                }
                break;
        }
    }
}
